Namespacing everything to /UVa.
[andmenj-acm.git] / UVa / 11115 - Uncle Jack / 11115.2.cpp
blob0bfa501813c9167e45aa204d5f700ec94e5cfff1
1 #include <stdio.h>
2 #include <iostream>
3 using namespace std;
5 const int MAX = 26;
7 class HugeInt {
8 public:
10 // vars
11 char number[MAX];
12 int length;
14 // constructors
15 HugeInt() {
16 memset(number,0,sizeof(number));
17 length = 1;
20 HugeInt(int n) {
21 *this = n;
24 // operators
25 // takes 'r' as right number and writes result to 'this'
26 // *veryfied*
27 HugeInt* operator= (const HugeInt* r) {
28 memset(number,0,sizeof(number));
29 strcpy(number,r->number);
30 length = r->length;
31 return this;
34 // *veryfied*
35 HugeInt* operator= (const int r) {
36 memset(number,0,sizeof(number));
37 sprintf(number,"%d",r);
38 length = strlen(number);
39 for (int i = 0; i < (length >> 1); i++) {
40 char c = number[i];
41 number[i] = number[length-i-1];
42 number[length-i-1] = c;
44 for (int j = 0; j < length; j++)
45 number[j] -= '0';
46 return this;
49 // *veryfied*
50 const HugeInt operator+ (const HugeInt& r) {
51 int n = max(length,r.length), carry = 0, k, i;
52 HugeInt theNew;
53 for (i = 0; i < n || carry; i++) {
54 k = number[i] + r.number[i] + carry;
55 theNew.number[i] = k % 10;
56 carry = k / 10;
58 theNew.length = i;
59 return theNew;
62 // slightly veryfied
63 void operator+= (const HugeInt& r) {
64 *this = *this + r;
67 // *slightly veryfied*
68 const HugeInt operator* (const int r) {
69 int i, k, carry = 0;
70 HugeInt theNew;
71 for (i = 0; i < length || carry; i++) {
72 k = number[i] * r + carry;
73 theNew.number[i] = k % 10;
74 carry = k / 10;
76 theNew.length = i;
77 return theNew;
80 // *slightly veryfied*
81 void operator*= (const int r) {
82 *this = *this * r;
83 //return this;
86 // shifts number left by 'shift' positions
87 // useful when multiplying two HugeInt's
88 HugeInt operator<< (const int shift) {
89 int i;
90 HugeInt theNew;
91 // don't shift if number is 0 and there is no number
92 //if (length == 0 || length == 1 && number[0] == 0) return;
93 for (i = length - 1; i >= 0; i--)
94 theNew.number[i + shift] = number[i];
95 for (i = 0; i < shift; i++)
96 theNew.number[i] = 0;
97 theNew.length = length + shift;
98 return theNew;
101 HugeInt operator* (HugeInt& r) {
102 int i;
103 HugeInt theNew = 0;
104 for (i = 0; i < length; i++)
105 theNew += (r << i) * number[i];
106 truncate(theNew);
107 return theNew;
110 HugeInt operator*= (HugeInt& r) {
111 *this = *this * r;
112 return *this;
115 int operator % (int r) {
116 int n = 0;
117 for (int i = length - 1; i >= 0; i--)
118 n = (n * 10 + number[i]) % r;
119 return n;
122 int operator %= (int r) {
123 return (*this % r);
126 HugeInt operator/ (int r) {
127 HugeInt I = 0;
128 int n = 0;
129 int j = 0;
130 for (int i = length - 1; i >= 0 || n >= r; i--) {
131 n = (n * 10 + number[i]);
132 I.number[j++] = n / r;
133 n %= r;
136 // reverse string
137 for (int i = 0; i < j / 2; i++)
138 swap(I.number[i],I.number[j-i-1]);
139 I.length = j;
140 truncate(I);
142 return I;
145 HugeInt operator /= (int r) {
146 return (*this / r);
149 // functions
150 // *veryfied*
151 void print() {
152 for (int i = length - 1; i >= 0; i--)
153 putchar(number[i] + '0');
156 void truncate(HugeInt& n) {
157 for (int i = n.length - 1; i >= 0 && n.number[i] == 0; i--)
158 n.length--;
159 if (n.length == 0) {
160 n.number[0] = 0;
161 n.length = 1;
165 HugeInt power(int p) {
166 HugeInt theNew = 1;
167 for (int i = 0; i < p; i++)
168 theNew *= *this;
169 return theNew;
172 bool operator<(const HugeInt& rhs) {
173 if (this->length != rhs.length)
174 return this->length < rhs.length;
175 for (int i = 0; i < this->length; i++)
176 if (this->number[i] != rhs.number[i])
177 return this->number[i] < rhs.number[i];
178 return false;
181 bool isnull() {
182 if (length == 1 && number[0] == 0) {
183 return true;
185 return false;
191 int main(){
192 int n, d;
193 while (cin >> n >> d && (n + d)){
194 if (n == 1 || d == 0){
195 printf("1\n");
196 }else{
197 HugeInt r(n);
198 r.power(d).print();
199 printf("\n");
202 return 0;